// components/index_my/index_my.js
import tool from '../../modules/tool'

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    userButtons: [],
    optionButtons: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleCardClick() {

      tool.postFrontStatus(`点击卡片`)
    },
    handleShowCreate() {
      tool.postFrontStatus(`点击我创建的`)
      tool.page.searchPatrol({
        patrolAccount: this.data.userData.account
      })
    },
    handleShowHandle() {
      tool.postFrontStatus(`点击我处理的`)
      tool.page.searchPatrol({
        handlerAccount: this.data.userData.account
      })
    },
    handleShowReview() {
      tool.postFrontStatus(`点击我审核的`)
      tool.page.searchPatrol({
        reviewerAccount: this.data.userData.account
      })
    },
    handleShowRemain() {
      tool.postFrontStatus(`点击今日剩余`)
      tool.handleShowToast(`今天你还可以新建${this.data.userData.patrol.remain}个巡查`, 'none')
    },
    async init() {
      tool.setPageData(this)

      this.setData({
        userButtons: [
          ...await tool.execFunc(async () => {
            if (tool.store.getItem('userData') && tool.store.getItem('userData').isAdmin) {
              return [{
                title: '管理员后台',
                callBack: tool.page.adminPage,
                icon: 'admin'
              }]
            } else {
              return []
            }
          }),
          {
            title: '个人资料',
            callBack: tool.page.editUserData,
            icon: 'my'
          },
          {
            title: '修改密码',
            callBack: tool.page.changePassword,
            icon: 'password'
          },
          {
            title: '订阅提醒',
            callBack: tool.handleSubWXNotif,
            icon: 'notif'
          },
          {
            title: '退出登录',
            icon: 'exit',
            callBack: () => tool.handleExitLogin(() => {
              tool.postFrontStatus(`点击退出登录`)
              tool.redirectToIndex()
            })
          },
        ],
        optionButtons: [{
          title: '使用旧版界面',
          icon: 'useOldUI',
          right: {
            type: 'switch',
            value: tool.store.getItem('useOldUI')
          },
          callBack(e, v) {
            wx.setStorageSync('useOldUI', v)
            tool.store.setItem('useOldUI', v)
            e()
          }
        }]
      })
    }
  },

  async attached() {
    this.init()
    tool.handleUpdateUserData()
    tool.store.onChange('userData', () => this.init())
  }
})